package com.mass.core.framework.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.ObjectUtils;

import com.mass.core.sys.role.model.SysRole;
import com.mass.core.sys.user.model.SysUser;

/**
 * 安全用户模型
 *
 */
public class SecurityUser implements UserDetails { 
	
    private static final long serialVersionUID = 1L; 
    
    private SysUser sysUser;
    
    private String userName;
    
    private String passWord;
    
    public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public SecurityUser(SysUser suser) {  
        this.sysUser = suser;       
    }  

    public SecurityUser() {
		super();
	}

	@Override
	public String toString() {
		return this.userName;
	}
	
	@Override
	public int hashCode() {
		return userName.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		return this.toString().equals(obj.toString());
	}
      
      
    public Collection<? extends GrantedAuthority> getAuthorities() {  
        if(!ObjectUtils.isEmpty(sysUser)){
        	Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();  
            List<SysRole> userRoles = sysUser.getSysRoleList();
              
            if(userRoles != null)  
            {  
                for (SysRole role : userRoles) {  
                    SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getRoleName());  
                    authorities.add(authority);  
                }  
            }  
            return authorities;  
        }  else {
        	Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();  
        	return authorities;
        }
    }  
  
      
    public String getPassword() {
    	if(!ObjectUtils.isEmpty(sysUser)){
    		return sysUser.getPassword();  
    	}else{
    		return passWord;
    	}
        
    }  
  
      
    public String getUsername() { 
    	if(!ObjectUtils.isEmpty(sysUser)){
    		return sysUser.getAccount();
    	}else{
    		return userName;
    	}
    }  
  
      
    public boolean isAccountNonExpired() {  
        return true;  
    }  
  
      
    public boolean isAccountNonLocked() {  
        return true;  
    }  
  
      
    public boolean isCredentialsNonExpired() {  
        return true;  
    }  
  
      
    public boolean isEnabled() {  
        return true;  
    }

	public SysUser getSysUser() {
		return sysUser;
	}

	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}  
    
    
}  
